import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
} from '@nestjs/common';
import ExamRoomService from '../service/ExamRoomService';
import ExamRoomQueryParam from '../param/ExamRoomQueryParam';
import ExamRoomVo from '../vo/ExamRoomVo';
import { R } from 'src/common/R';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { Log } from 'src/common/decorator/LogDecorator';
import BusinessType from 'src/common/enum/BusinessType';
import ExamRoomDto from '../dto/ExamRoomDto';

/*
 * @Author: jingfuu@163.com
 * @Date: 2024-12-16 12:49:44
 * @LastEditTime: 2024-12-16 13:11:13
 * @Description: 考场
 */

@ApiTags('考场接口')
@Controller('exam/room')
class ExamRoomController {
  constructor(private examRoomService: ExamRoomService) {}

  @ApiOperation({ summary: '获取考场列表' })
  @Get('list')
  async list(@Query() param: ExamRoomQueryParam): Promise<R<ExamRoomVo>> {
    return R.page(await this.examRoomService.list(param));
  }

  @ApiOperation({ summary: '获取考场详情' })
  @Get(':id(\\d+)')
  async findDetail(@Param('id') id: number): Promise<R<ExamRoomVo>> {
    return R.ok(await this.examRoomService.detail(id));
  }

  @ApiOperation({ summary: '新增考场' })
  @Log({
    title: '考场',
    businessType: BusinessType.INSERT,
  })
  @Post()
  async add(@Body() examRoom: ExamRoomDto): Promise<R<void>> {
    await this.examRoomService.add(examRoom);
    return R.ok();
  }

  @ApiOperation({ summary: '修改考场' })
  @Log({
    title: '考场',
    businessType: BusinessType.UPDATE,
  })
  @Put()
  async modifyExamDistrict(@Body() examRoom: ExamRoomDto): Promise<R<void>> {
    await this.examRoomService.edit(examRoom);
    return R.ok();
  }

  @ApiOperation({ summary: '删除考场' })
  @Log({
    title: '考场',
    businessType: BusinessType.DELETE,
  })
  @Delete('/:id')
  async remove(@Param('id') id: string): Promise<R<any>> {
    if (id.indexOf(',') > -1) {
      for (const item of id.split(',')) {
        await this.examRoomService.remove(Number.parseInt(item));
      }
    } else {
      await this.examRoomService.remove(Number.parseInt(id));
    }
    return R.ok();
  }
}
export default ExamRoomController;
